package com.huazai.yangge.completable.future;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

/**
 * @author 华仔
 * @date 2024/2/21
 */
public class CompletableFutureCombineDemo {

    // 合并连个线程的结果
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        final CompletableFuture<String> play1 = CompletableFuture.supplyAsync(() -> {
            System.out.println("111 = ");
            try {
                TimeUnit.MILLISECONDS.sleep(100);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            return "play 111";
        });

        final CompletableFuture<String> play2 = CompletableFuture.supplyAsync(() -> {
            System.out.println("222 = ");
            try {
                TimeUnit.MILLISECONDS.sleep(200);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            return "play 222";
        });
        final CompletableFuture<String> stringCompletableFuture = play1.thenCombine(play2, (s, s2) -> s + s2);
        System.out.println("合并结果 = " + stringCompletableFuture.join());

    }
}

// thenRun 任务A执行完执行任务B， 并且B不需要A的结果
// thenAccept 任务A执行完执行任务B， B需要A的结果， 但是B无返回值
// thenApply 任务A执行完执行任务B， B需要A的结果 同时任务B有返回值






